package com.codegen.controller;

import com.codegen.entity.TableGenParam;
import com.codegen.service.TableGenParamService;
import com.codegen.utils.PageUtils;
import com.codegen.utils.Query;
import com.codegen.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/project")
@Validated
public class ProjectModuleController {

    @Autowired
    private TableGenParamService tableGenParamService;

    /**
     * 新增项目
     * @return
     */
    @PostMapping("/add")
    public R addProject(@Validated(TableGenParam.Add.class) @RequestBody TableGenParam tableGenParam){

        int result = tableGenParamService.addProject(tableGenParam);
        return result > 0 ? R.ok() : R.error("新增失败");
    }

    /**
     * 修改项目
     * @param tableGenParam
     * @return
     */
    @PostMapping("/edit")
    public R editProject(@Validated(TableGenParam.Edit.class) @RequestBody TableGenParam tableGenParam){
        int result = tableGenParamService.editProject(tableGenParam);
        return result > 0 ? R.ok() : R.error("修改失败");
    }

    /**
     * 根据项目ID查询
     * @param projectId
     * @return
     */
    @GetMapping("/info/{projectId}")
    public R getInfo(@PathVariable("projectId") Integer projectId){
        TableGenParam tableGenParam = tableGenParamService.selectOneByProjectId(projectId);
        return R.ok().put("data",tableGenParam);
    }

    /**
     * 查询所有项目
     * @return
     */
    @GetMapping("/list")
    public R getList(){
        List<TableGenParam> list = tableGenParamService.selectAll();
        return R.ok().put("data",list);
    }

    /**
     * 分页查询
     * @param params
     * @return
     */
    @GetMapping("/page")
    public R getPage(@RequestParam Map<String, Object> params){
        PageUtils pageUtil = tableGenParamService.queryList(new Query(params));
        return R.ok().put("data",pageUtil);
    }
}
